<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/4/13
 * Time: 11:57
 */

class Apply extends M_Controller
{
    public function main(){

        list($data, $total, $param)	= $this->_limit_page(); // 数据库中分页查询

        $id = (int)$param['id'];
        $this->load->library('dip');

        //截止当前会员统计
        $today = date('Y-m-d 00:00:00',time());
        $latest = date('Y-m-d H:i:s',time());
//        $today = '2018-08-03 00:00:00';
//        $latest = '2018-08-03 23:59:59';
        $resmem = $this->db->where('addtime >=' ,$today)->where('addtime <=' ,$latest)->get('try_apply')->result_array();
        $countmem = count($resmem);

        $this->template->assign(array(
            'list'=>$data,
            'countmem' =>$countmem,
			'total' => $total,
			'param' => $param,
			'auser' => $this->db->where('id', $id)->get('try_apply')->row_array(),
			'pages'	=> $this->get_pagination(dr_url('admin/apply/main', array(
            'uid' => $id,
            'search' => IS_POST ? 1 : $this->input->get('search')
        )), $total)
        ));
        $this->template->display('apply/try_apply.html');


    }
    private function _where(&$select) {

        $uid = (int)$this->input->get('id');
        $uid = $uid ? $uid : $this->uid;
        $search = $this->input->get('search');
        $cache_file = md5($this->duri->uri(1).$uid.$this->uid.SITE_ID.$this->input->ip_address().$this->input->user_agent()); // 缓存文件名称

        // 存在POST提交时，重新生成缓存文件
        if (IS_POST) {
            $data = $this->input->post('data');
            $this->cache->file->save($cache_file, $data, 3600);
            $search = 1;
            unset($_GET['page']);
        }

        $select->where('id', $uid);

        // 存在search参数时，读取缓存文件
        if ($search == 1) {
            $data = $this->cache->file->get($cache_file);
            if (isset($data['start']) && $data['start'] && $data['start'] != $data['end']) {
                $data['end'] = $data['end'] ? $data['end'] : SYS_TIME;
                $select->where('logintime BETWEEN '.$data['start'].' AND '. $data['end']);
            }
        }

        $data['id'] = $uid;

        return $data;
    }

    private function  _limit_page(){
        $page = max((int)$this->input->get_post('page'),1);
        $_total = (int)$this->input->get_post('total');

        if(!$_total){
//            $select = $this->db;
//            $_param = $this->_where($select);
            $_total = $this->db->count_all_results('try_apply');
            if(!$_total){
                return array();
            }
        }

        $select = $this->db;
//        $_param = $this->_where($select);
        $result = $this->db
                   ->limit(SITE_ADMIN_PAGESIZE, SITE_ADMIN_PAGESIZE * ($page - 1))
                    ->order_by('id desc')
                    ->get('try_apply')
                    ->result_array();
        return array($result,$_total, $_param);
    }

    public function excelData(){
        $dateone = $_POST['dateone'];
        $datetwo = $this->input->post('datetwo');

        header("Content-type:text/html");
        header("Content-Disposition:attachment;filename=试用申请.xls");
        if (!empty($dateone)&&!empty($datetwo)){
        $array=$this->db->where('addtime >' ,$dateone)->where('addtime <' ,$datetwo)->get("try_apply")->result_array();
        }else{
            $array=$this->db->get("try_apply")->result_array();
        }
        $str="id\t"."注册时间\t"."姓名\t"."手机号\t"."活动\t"."来源\t"."年级\t"."学科\n";
        foreach($array as $val){
            $str.=$val['id']."\t".$val['addtime']."\t".$val['name']."\t".$val['mobile']."\t".$val['remark']."\t".$val['source']."\t".$val['grade']."\t".$val['subject']."\n";
        }
        echo $str;
        }


}